package com.smartthinking.destinomusical.parser;

import java.io.IOException;
import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Log;
import android.util.Xml;

import com.smartthinking.destinomusical.extra.Errores;
import com.smartthinking.destinomusical.obj.Suscripciones;

public class ParserSuscripciones extends BaseFeedParser {
	final ArrayList<Suscripciones> lista = new ArrayList<Suscripciones>();
	static final String ROOT = "RESULTS";
	int total;
	Suscripciones obj;

	public ParserSuscripciones(String feedUrl) {
		super(feedUrl);
	}

	public ArrayList<Suscripciones> parse() {

		RootElement root = new RootElement(ROOT);

		root.getChild("TOTAL").setStartElementListener(
				new StartElementListener() {
					@Override
					public void start(Attributes attributes) {
						total = Integer.valueOf(attributes.getValue("rows"));
					}
				});

		
			
			Element row = root.getChild("ROW");
			row.setStartElementListener(new StartElementListener() {

				@Override
				public void start(Attributes arg0) {
					obj =  new Suscripciones();

				}
			});
			
			row.getChild("cli_idr").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("cli_idr:" + body);
							obj.setIdr(Integer.parseInt(body));
						}
					});
			row.getChild("sus_cons").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("sus_cons:" + body);
							obj.setCons(Integer.parseInt(body));
						}
					});
			row.getChild("sus_tipo").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("sus_tipo url:" + body);
							obj.setTipo(body);
						}
					});
			row.getChild("sus_fec_ini").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("sus_fec_ini:" + body);
							obj.setFecha_ini(body);
						}
					});
			row.getChild("sus_fec_fin").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("sus_fec_fin:" + body);
							obj.setFecha_fin(body);
						}
					});
			row.getChild("status").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("status:" + body);
							obj.setStatus(body);
						}
					});
			row.setEndElementListener(new EndElementListener() {

				@Override
				public void end() {
					lista.add(obj);

				}
			});
		
		try {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,
					root.getContentHandler());
		} catch (IOException e) {
			Errores.NO_CONNECTION = 1;
			Log.e("ParserSuscripciones", e.getMessage());
		} catch (SAXException e) {
			Log.e("ParserSuscripciones", e.getMessage());
		} catch (Exception e) {
			if (e.getLocalizedMessage() != null) {
				Log.e("ParserSuscripciones", e.getLocalizedMessage());
				Errores.NO_CONNECTION = 1;
			} else
				Log.e("ParserSuscripciones", "Unknown Exception");
		}
		return lista;
	}
}
